package com.lianxi.io;


import com.mysql.cj.util.Base64Decoder;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class IoTest {


    public static void main(String[] args) throws IOException {
//        ioAvailable();
//        ioInputStream();
//        ioOutStream();
//        ioCopy();
//        ioBufferReader();


        File file = new File("D:\\Desktop\\demo.txt");
        System.out.println(File.separator);
        System.out.println(1);

    }

    public static void ioInputStream() throws IOException {
        FileInputStream is = new FileInputStream("D:\\Desktop\\测试demo.txt");
        System.out.println(is.available()); //文件总字节数量
        byte[] bytes = new byte[4];//每次读取到的字符数量，一次最多读取4个字节，到byte当中
        int result;

        while ((result = (is.read(bytes))) != -1) {
            /**
             *  为什么不能这样写？因为每次读取byte数组时，其实是进行替换。
             *              最后一次有可能读的字节数《上一次的字节，会造成替换不充分。
             */
//           String s = new String(bytes);

            // 不应该全部都转换，应该是读取了多少个字节，转换多少个。
            String s = new String(bytes, 0, result);
            System.out.println(s);
        }
    }

    public static void ioAvailable() throws IOException {
        FileInputStream is = new FileInputStream("D:\\Desktop\\测试demo.txt");
        System.out.println("文件总字节数量：" + is.available()); //文件总字节数量
        byte[] bytes = new byte[is.available()];//
        is.read(bytes);
        System.out.println(new String(bytes));  //适合文件不大的情况下
    }


    public static void ioOutStream() throws IOException {
        File file = new File("D:\\Desktop\\测试");
        file.mkdirs(); //级联先创建文件夹
        System.out.println(file);
        FileOutputStream os = new FileOutputStream(file + "\\测试.txt", true); //append=true是否在文件夹追加
        byte[] bytes = new byte[]{97, 98, 99, 100};
        //追加写写中文
        String s = new String("我是一个中国人");
        os.write(s.getBytes());
        os.flush();
        os.close();
    }


    //手写复制,先读，后写。
    public static void ioCopy() throws IOException {
        FileInputStream is = new FileInputStream("D:\\Desktop\\测试demo.txt");
        FileOutputStream os = new FileOutputStream("D:\\Desktop\\测试demo2.txt");
        int readLoad;
        byte[] bytes = new byte[4];
        while ((readLoad = (is.read(bytes))) != -1) {
            os.write(bytes, 0, readLoad);
        }
        is.close();
        os.close();
    }


    public static void ioBufferReader() throws IOException {
        FileInputStream is = new FileInputStream("D:\\Desktop\\测试demo.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
        byte[] bytes = new byte[1024];
        int readCount;
        while ((readCount = (bufferedInputStream.read(bytes))) != -1) {
            System.out.println(new String(bytes, 0, readCount));
        }
    }


    //根据url下载图片
    public static void download(String urlString, String name) throws Exception {
        URL url = new URL(urlString);
        URLConnection con = url.openConnection();
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        String filename = "D:\\图片下载\\" + name;  //下载路径及下载图片名称
        File file = new File(filename);
        FileOutputStream os = new FileOutputStream(file, true);
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        System.out.println(filename + "下载成功");
        // 完毕，关闭所有链接
        os.close();
        is.close();
    }


    //浏览器下载，见com.lianxi.excel.excleController


}


class FileUtilsTest {

    public static void main(String[] args) throws IOException {
//        download("https://profile.csdnimg.cn/E/5/4/2_qq_44828156","E:\\DeskTop\\2_qq_44828156.png");
        int i = 0;
        while (true) {
            if (i == 2) {
                break;
            }
            testWrite();
            i++;
        }

    }

    //根据url下载图片
    public static void download(String urlString, String name) {
        try {
            FileUtils.copyURLToFile(new URL(urlString), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * /n 回车
     * /r 空格
     * /t 跳格
     * @throws IOException
     */
    public static void testWrite() throws IOException {
        List<String> strings = Arrays.asList("1","2","3","4");
        FileUtils.writeLines(new File("E:\\DeskTop\\demo.txt"), strings, "\n", true);
    }


}


class FileBase64Test {

    public static void main(String[] args) throws IOException {
        ioRead();
    }


    public static void ioRead() throws IOException {
        FileInputStream is = new FileInputStream("E:\\Desktop\\互联互通20220727(2)(1).docx");
        FileOutputStream os = new FileOutputStream("E:\\Desktop\\互联互通.docx");
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(bytes);
        System.out.println(">>>>>>>>>>开始解析<<<<<<<<<<");
        BASE64Decoder decoder=new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(encode);
        os.write(bytes1);


    }



}
